﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace ProjectEdificio
{
    public partial class NewUser : Window
    {
        private Usuario u = null;
        private Dictionary<String, Usuario> users = null;
        private Dictionary<String, Usuario> usersEmail = null;
        private BDManagment bdm = BDManagment.getInstance();
        private bool flag; // true para new, false para edit
        private string userOld;
        private Authentication auti = null;



        public NewUser(bool flag)
        {
            InitializeComponent();
            users = bdm.getListUsers();
            usersEmail = bdm.getListEmails();
            this.textBoxUser.Focus();
            this.flag = flag;
            this.Title = "Nuevo usuario";
        }

        public NewUser(bool flag, string u, string p, string r, string j, string e)
        {
            InitializeComponent();
            auti = Authentication.getInstance();
            users = bdm.getListUsers();
            this.flag = false;
            this.userOld = u;
            this.textBoxUser.Text = u;
            this.textBoxPass.Password = p;
            this.textBoxPass2.Password = p;
            if (r == "Administrador")
            {
                this.comboBoxRange.SelectedIndex = 0;
            }
            else
            {
                this.comboBoxRange.SelectedIndex = 1;
            }
            this.textBoxJob.Text = j;
            this.textBoxEmail.Text = e;
            this.textBoxUser.Focus();
            this.flag = flag;
            this.Title = "Editar Usuario";
        }

        private void buttonAceptar_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxUser.Text == string.Empty || textBoxPass.Password == string.Empty || textBoxPass2.Password == string.Empty || textBoxEmail.Text == string.Empty || textBoxJob.Text == string.Empty || comboBoxRange.SelectedItem == null)
            {
                MessageBox.Show("Llene todos los campos!");
                textBoxPass.Password.DefaultIfEmpty();
                textBoxPass2.Password.DefaultIfEmpty();
                this.textBoxUser.Focus();
            }
            else
            {
                if (textBoxPass.Password != textBoxPass2.Password)
                {
                    MessageBox.Show("Las contraseñas no coinciden!");
                    textBoxPass.Password.DefaultIfEmpty();
                    textBoxPass2.Password.DefaultIfEmpty();
                    this.textBoxPass.Focus();
                }
                else
                {
                    if (flag) //new
                    {
                        if (users.ContainsKey(textBoxUser.Text))
                        {
                            MessageBox.Show("Ya existe un usuario con ese nombre");
                            textBoxPass.Password.DefaultIfEmpty();
                            textBoxPass2.Password.DefaultIfEmpty();
                            this.textBoxUser.Focus();
                        }
                        else
                        {
                            if(usersEmail.ContainsKey(textBoxEmail.Text))
                            {
                                MessageBox.Show("Ya existe ese Email!");
                            }
                            else
                            {
                                u = new Usuario(textBoxUser.Text.ToLower(), textBoxPass.Password, comboBoxRange.Text, textBoxEmail.Text, textBoxJob.Text);
                                int i = bdm.addUser(u);
                                if (i == 1)
                                {
                                    MessageBox.Show("Se ha creado con exito un nuevo usuario!");
                                }
                                else
                                {
                                    MessageBox.Show("No se ah podido agregar el usuario!");
                                    textBoxPass.Password.DefaultIfEmpty();
                                    textBoxPass2.Password.DefaultIfEmpty();
                                }

                                this.Close();
                        }
                        }
                            
                    }
                    else //edit
                    {
                        if (userOld == textBoxUser.Text)
                        {
                            bdm.editUser(userOld, textBoxUser.Text, textBoxPass.Password, comboBoxRange.Text, textBoxJob.Text, textBoxEmail.Text);
                        }
                        else
                        {
                            if (users.ContainsKey(textBoxUser.Text))
                            {
                                MessageBox.Show("Ya existe ese usuario!");
                            }
                            else
                            {
                                int i = bdm.editUser(userOld, textBoxUser.Text, textBoxPass.Password, comboBoxRange.Text, textBoxJob.Text, textBoxEmail.Text);
                                if (i == 1)
                                {
                                    MessageBox.Show("Modificación exitosa!");
                                    this.Close();
                                }
                                else
                                {
                                    MessageBox.Show("Operación Fallida");
                                }
                            }
                        }
                    }
                }
            }
        }

        private void textBoxPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (textBoxPass.Password != textBoxPass2.Password)
            {
                this.imageBad.Visibility = Visibility.Visible;
                this.imageGood.Visibility = Visibility.Hidden;
            }
            else
            {
                this.imageGood.Visibility = Visibility.Visible;
                this.imageBad.Visibility = Visibility.Hidden;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
